package com.jsmile.cloud.authc.sys.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysRolePageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysRoleQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysRoleUpdate;
import com.jsmile.cloud.authc.api.res.ResSysResources;
import com.jsmile.cloud.authc.api.res.ResSysRole;
import com.jsmile.cloud.authc.sys.SysRoleApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "角色管理")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/role")
public class SysRoleController extends AuthcController {

    private final SysRoleApplicationService sysRoleApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResSysRole>> getPageList(@RequestBody @Valid ReqSysRolePageQuery reqSysRolePageQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysRolePageQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysRole> pages = sysRoleApplicationService.getPageList(reqSysRolePageQuery);
        return JSmileResult.success(new JsmilePage(reqSysRolePageQuery.getPageIndex(), reqSysRolePageQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @ApiOperation(value = "根据角色id获取角色资源信息")
    @RequestMapping(value = "/getRoleResources", method = {RequestMethod.POST})
    public JSmileResult getRoleResource(@RequestBody @Valid ReqSysRoleQuery reqSysRoleQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysRoleQuery.setCurTenantId(jsmileContext.getTenantId());
        List<ResSysResources> list = sysRoleApplicationService.getMenuByRoles(reqSysRoleQuery);
        return JSmileResult.success(list);
    }

    @ApiOperation(value = "保存角色资源信息")
    @RequestMapping(value = "/saveRoleResources", method = {RequestMethod.POST})
    public JSmileResult saveRoleResources(@RequestBody @Valid ReqSysRoleUpdate reqSysRoleUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysRoleUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysRoleUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysRoleUpdate.setUpdateBy(jsmileContext.getUserName());
        sysRoleApplicationService.saveRoleResources(reqSysRoleUpdate);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增角色")
    @JsmileLog(title = "用户中心->新增角色", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveRole(@RequestBody @Valid ReqSysRoleUpdate reqSysRoleUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysRoleUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysRoleUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysRoleUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysRoleApplicationService.saveRole(reqSysRoleUpdate)) {
            return JSmileResult.error("添加角色异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改角色")
    @JsmileLog(title = "用户中心->修改角色", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateRole(@RequestBody @Valid ReqSysRoleUpdate reqSysRoleUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysRoleUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysRoleUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysRoleUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysRoleApplicationService.updateRole(reqSysRoleUpdate)) {
            return JSmileResult.error("修改角色异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除角色")
    @JsmileLog(title = "用户中心->删除角色", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteRole(@RequestBody @Valid ReqSysRoleUpdate reqSysRoleUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysRoleUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysRoleApplicationService.deleteRole(reqSysRoleUpdate)) {
            return JSmileResult.error("删除角色异常!");
        }
        return JSmileResult.success();
    }
}
